$server = 'http://localhost:8080/';

$(function () {
    zhuce(
        {
            use : $('.user input'),
            u : $('.user_hint'),
            obj : $('.pwd'),
            h : $('.password_hint'),
            reobj :$('.repwd'),
            j : $('.confirm_hint'),
            ZC : $('.register a'),
            mobile : $('.iphone input'),
            m : $('.iphone_hint'),
            yanzheng : $('.iphone_IC button'),
            num : $('.iphone_IC_hint'),
            sj : $('.iphone_IC_hint span'),
			forget_question : $('.forget_question input'),
			forget_question_h : $('.forget_question_hint'),
			forget_answer : $('.forget_answer input'),
			forget_answer_h : $('.forget_answer_hint'),
        }
    )

});
function zhuce(json) {
    var use = json.use;
    var u = json.u;
    var obj = json.obj;
    var h = json.h;
    var reobj = json.reobj;
    var j = json.j;
    var ZC = json.ZC;
    var mobile = json.mobile;
    var m = json.m;
    var yanzheng = json.yanzheng;
    var num = json.num;
    var sj = json.sj;
    var act = json.act;
    var user_Boolean = false;
    var password_Boolean = false;
    var varconfirm_Boolean = false;
    var iphone_hint = false;
    var s = 0;
	
	var forget_question = json.forget_question;
	var forget_question_h = json.forget_question_h;
	var forget_answer = json.forget_answer;
	var forget_answer_h = json.forget_answer_h;
	var forget_question_Boolean = false;
	var forget_answer_Boolean = false;
	
	$(forget_question).blur(function(){
		if ($(forget_question).val() != null && $(forget_question).val() != '' && $(forget_question).val() != undefined){
			$(forget_question_h).html("✔").css("color",'green');
			forget_question_Boolean = true;
		}else{
			$(forget_question_h).html("x").css("color", "red");
			forget_question_Boolean = false;
		}
	});
	
	$(forget_answer).blur(function(){
		if ($(forget_answer).val() != null && $(forget_answer).val() != '' && $(forget_answer).val() != undefined){
			$(forget_answer_h).html("✔").css("color",'green');
			forget_answer_Boolean = true;
		}else{
			$(forget_answer_h).html("x").css("color", "red");
			forget_answer_Boolean = false;
		}
	});
	
    $(use).blur(function () {
        if ((/^[\u4e00-\u9fa5]{3,14}$|^[\dA-Za-z_]{6,28}$/).test($(use).val())){
            $(u).html("✔").css("color",'green');
            user_Boolean = true;
        }
        else {

            $(u).html("×").css("color","red");
            user_Boolean = false;
        }
    });
    // 密码
    $(obj).blur(function () {
        if((/^[a-z0-9_-]{6,16}$/).test($(obj).val())){
            $(h).html("✔").css("color",'green');
            password_Boolean = true;
        }else {
            $(h).html("×").css("color","red");
            password_Boolean = false;
        }
    });
    // 确认密码
    $(reobj).blur(function () {
        if($(obj).val() === $(reobj).val()){
            $(j).html("✔").css("color",'green');
            varconfirm_Boolean = true;
        }else {
            $(j).html("×").css("color","red");
            varconfirm_Boolean = false;
        }
    });
    // 手机号码
    $(mobile).blur(function () {
        if((/^1[34578]\d{9}$/).test($(mobile).val())){
            $(m).html("✔").css("color",'green');
            iphone_hint = true;
            s = 1;
        }
        else {
            $(m).html("×").css("color","red");
            iphone_hint = false;
            s = 0;
        }
    });
    //手机验证码
    $(yanzheng).click(function () {
            $(this).css({display:'none'});
            $(num).css({display:"block"});
            var time =60;
            var sj=null;
            sj = setInterval(function () {
                time-=1;
                if(time >0){
                    $(num).html(time+'秒后重发')
                }else {
                    // time = 5;
                    clearInterval(sj);
                    $(yanzheng).css({display:"block"});
                    $(num).css({display:'none'});
                }

            },1000);
            $(num).html(time+'秒后重发');

    });
    // 点击注册
    $(ZC).click(function () {
		if (!validateCode())
			return;
			
		$password = $(obj).val();
		$username = $(use).val();
		$passwordRe = $(reobj).val();  //已经校验，这里不用校验
		$question = $(forget_question).val();
		$answer = $(forget_answer).val();
		
        if(user_Boolean === true && password_Boolean === true && varconfirm_Boolean === true && forget_question_Boolean == true && forget_answer_Boolean == true){
			$.ajax({
				url: $server + 'user/register.do',
				datatype: 'json',
				data: {username:$username, password: $password, question: $question, answer: $answer},
				type: 'POST',
				corssDomain: true,
				xhrFields:{
					withCredentials: true // 携带跨域cookie
				},
				success: function(result){
					console.log(result);
					if (result.code == 200){
						alert("注册成功，返回登录页面");
						window.location.href="login.html";
					}else{
						alert(result.msg);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(textStatus);
				}
			});
        }
        else if(user_Boolean === false){
            alert("注册名不规范")
        }
        else if(password_Boolean === false){
            alert("请输入密码")
        }
        else if(varconfirm_Boolean ===false){
            alert("请确认密码一致")
        }
        else if(forget_question_Boolean === false || forget_answer_Boolean == false){
            alert("请填写用于找回密码的问题答案")
        }
    });
    //点击登录
    $(act).click(function () {	
		if (!validateCode())
			return;
        if(user_Boolean === true && password_Boolean === true){
			$password = $(obj).val();
			$username = $(use).val();
			
			$.ajax(
			{
				url: $server+'user/login.do',
				type: "POST",
				datatype: 'json',
				data: { username: $username, password: $password},			
				crossDomain:true, //设置跨域为true
				xhrFields:{
					withCredentials: true // 携带跨域cookie
				},
				success: function(result){
					console.log(result);
					if (result.code == 200){
						window.location.href="index.html";
						// $(this).attr("href","index.html")
					}else{
						alert(result.msg);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(textStatus);
				}
			});
        }
        else if(user_Boolean === false){
            alert("请填写用户名")
        }
        else if(password_Boolean === false){
            alert("请确认密码")
        }
		createCode(4);
    })
}

//检查验证码是否正确
    function validateCode()
    {
        //获取显示区生成的验证码
        var checkCode = document.getElementById("checkCode").innerHTML;
        //获取输入的验证码
        var inputCode = document.getElementById("inputCode").value;
        if (inputCode.length <= 0)
        {
            alert("请输入验证码！");
        }
        else if (inputCode.toUpperCase() != checkCode.toUpperCase())
        {
            alert("验证码输入有误！");
            createCode(4);
			return false;
        }
        else
        {
            return true;
        }       
    }  